// import { format } from "util";

/**
 * 兑换码验证页面
 */
Ext.define("PRO.shop.view.cdkey.shopCDKeyView",{
    extend: 'Ext.panel.Panel',
    border:0,
    layout:'fit',
    alias: 'widget.shop.shopcdkeyview',
    autoScroll : true,
    initComponent: function(){
        var me = this;
        me.phoneType='0';
        me.items=[{
            xtype:"form",
            border:0,
            layout:'column',
            items:[{
                xtype:'textfield',
                columnWidth: 1,
                itemId:'subVoucherNo',
                fieldStyle:'font-size:16px;',
                emptyText:'请输入兑换码',
                cls:'setHolderStyle',
                name:'number',
                // maxLength:12,
                height:45,
                listeners:{
                    blur:function(view,evObj,obj){
                        var form =view.up('panel[xtype=form]');
                        var numberHint = form.getCmp('numberHint');//phoneNo
                        var phoneHint = form.getCmp('phoneHint');
                        var phoneCode = form.getCmp('phoneCode');
                        var phoneNo=form.getCmp('phoneNo');//phoneNo
                        var newVal = view.getValue();
                        if(JE.isEmpty(newVal)){
                            numberHint.update('兑换码不能为空！');
                            phoneNo.setValue('');
                        }else{
                            numberHint.update('');
                            form.subVoucherNo = newVal;
                            JE.ajax({
                                url:'/je/shoppingmall/voucher/getBindingPhoneNo',
                                params:{
                                    subVoucherNo:newVal
                                },
                                success:function(res){
                                    var res = JSON.parse(res.responseText);
                                    if(res.success){
                                        phoneNo.setValue(res.obj);
                                    }else{
                                        if(res.code=='705'){
                                            phoneHint.update(res.message);
                                        }else{
                                            numberHint.update(res.message||'兑换码无效！');
                                            phoneHint.update('');
                                        }
                                        
                                        phoneNo.setValue('');
                                    }
                                }
                            })
                        }
                    }
                }
            },{
                xtype:'box',
                itemId:'numberHint',
                margin:'10px 0 0 0',
                height:15,
                style: {
                    color: '#FF0000'
                },
                columnWidth: 1,
            },{
                xtype:'textfield',
                columnWidth: 1,
                margin:'10px 0 0 0',
                fieldStyle:'font-size:16px;',
                emptyText:'请输入密匙',
                name:'password',
                height:45,
                cls:'setHolderStyle',
                listeners:{
                    change:function(view,newVal,oldVal){
                        var form =view.up('panel[xtype=form]');
                        var passwordHint=form.getCmp('passwordHint');
                        if(JE.isEmpty(newVal)){
                            passwordHint.update('密匙不能为空!');
                        }else{
                            passwordHint.update('');
                        }
                    }
                }
            },{
                xtype:'box',
                itemId:'passwordHint',
                margin:'10px 0 0 0',
                height:15,
                style: {
                    color: '#FF0000'
                },
                columnWidth: 1,
            },{
                xtype:'textfield',
                itemId:'phoneNo',
                columnWidth: 1,
                margin:'10px 0 0 0',
                cls:'setHolderStyle',
                fieldStyle:'font-size:16px;',
                emptyText:'请输入手机号',
                readOnly:true,
                name:'phone',
                height:45,
                listeners:{
                    change:function(view,newVal,oldVal){
                        var form =view.up('panel[xtype=form]');
                        var phoneHint=form.getCmp('phoneHint');
                        if(JE.isEmpty(newVal)){
                            phoneHint.update('手机号不能为空!');
                        }else{
                            var re = /^1[34578]\d{9}$/;
                            if (!re.test(newVal)) {
                                phoneHint.update('手机号格式不正确!');
                            }else{
                                phoneHint.update('');
                                me.phoneType='1';
                            }   
                            
                        }
                    }
                }
            },{
                xtype:'box',
                itemId:'phoneHint',
                margin:'10px 0 0 0',
                height:15,
                style: {
                    color: '#FF0000'
                },
                columnWidth: 1,
            },{
                xtype:'textfield',
                columnWidth: 1,
                margin:'10px 0 0 0',
                cls:'setHolderStyle',
                fieldStyle:'font-size:16px;',
                emptyText:'请输入手机号验证码',
                itemId:'phoneCode',
                name:'phoneCode',
                height:45,
                listeners:{
                    afterrender:function(field){
                        var me = this;
                        field.inputEl.insertHtml('beforeBegin','<span class="phoneBtn" style="position: absolute;right: 10px;top: 15px;color: blue;font-size: 14px;cursor:pointer;">获取验证码</span>');
                        var form=field.up('panel[xtype=form]');
                        
                        var subVoucherNo = form.subVoucherNo;
                        form.getEl().on('click',function(e){
                            var subVoucherNo=field.up('panel[xtype=form]').subVoucherNo;
                            var phoneNo=field.up('panel[xtype=form]').getCmp('phoneNo').getValue();
                            if(e.getTarget('.phoneBtn',1)){
                                var phoneBtn = e.getTarget('.phoneBtn',1);
                                if(JE.isEmpty(phoneNo)) return JE.alert('请输入正确兑换码');
                                JE.ajax({
                                    url:'/je/shoppingmall/voucher/sendValidateCode',
                                    params:{
                                        subVoucherNo:subVoucherNo
                                    },
                                    success:function(res){
                                        var s = 60;
                                        var timer = setInterval(function(){
                                            s--;
                                            if(s==0){
                                                phoneBtn.innerHTML = '重新获取';
                                                clearInterval(timer)
                                            }else{
                                                phoneBtn.innerHTML = s+'s';
                                            }
                                        },1000)
                                    }
                                })
                            }
                         });
                    },
                    change:function(view,newVal,oldVal){
                        var form =view.up('panel[xtype=form]');
                        var phoneCodeHint=form.getCmp('phoneCodeHint');
                        if(JE.isEmpty(newVal)){
                            phoneCodeHint.update('验证码不能为空!');
                        }else{
                            phoneCodeHint.update('');
                        }
                    }
                }
            },{
                xtype:'box',
                itemId:'phoneCodeHint',
                margin:'10px 0 0 0',
                height:15,
                style: {
                    color: '#FF0000'
                },
                columnWidth: 1,
            },{
                xtype:'button',
                cls : 'btn JEPLUS_B_L10',
                margin:'10px 0 0 0',
                text:'确认',
                columnWidth: 1,
                height:40,
                handler:function(btn){
                    me.doSave(btn);
                }
            },{
                xtype:'dataview',
                margin:'20px 0 0 0',
                columnWidth:1,
                tpl:new Ext.XTemplate(
                    '<p style="font-size:14px;color:#999999;text-align: center;">兑换码包含已配置好的产品，无需再次购买，如需帮助可联系客服<font style="color:#000000">&nbsp;&nbsp;400-0999-235</font></p>'
                ), 
            }]
        }]
        me.callParent(arguments);
    },
    doSave:function(btn){
        var me=this;
        var form =btn.up('panel[xtype=form]');
        var vals=form.getValues();
        var numberHint=form.getCmp('numberHint');
        var passwordHint=form.getCmp('passwordHint');
        var phoneHint=form.getCmp('phoneHint');
        var phoneCodeHint=form.getCmp('phoneCodeHint');
        if(JE.isEmpty(vals.number)){
            numberHint.update('兑换码不能为空！');
        }
        if(JE.isEmpty(vals.password)){
            passwordHint.update('密匙不能为空!');
        }
        if(JE.isEmpty(vals.phone)){
            phoneHint.update('手机号不能为空!');
        }else{
            var re = /^1[34578]\d{9}$/;
            if (!re.test(vals.phone)) {
                phoneHint.update('手机号格式不正确!');
            }
        }
        if(JE.isEmpty(vals.phoneCode)){
            phoneCodeHint.update('验证码不能为空!');
        }
        if(JE.isNotEmpty(vals.number) && JE.isNotEmpty(vals.password) && JE.isNotEmpty(vals.phone) && JE.isNotEmpty(vals.phoneCode)){
            JE.ajax({
                url:'/je/shoppingmall/voucher/useSubvoucher',
                params:{
                    subVoucherNo:vals.number,
                    keyCode:vals.password,
                    phoneNo:vals.phone,
                    validateCode:vals.phoneCode
                },
                success:function(res){
                    var res = JSON.parse(res.responseText);
                    if(res.success){
                        me.up('window').close();//关闭窗口
                        JE.win({
                            xtype:'panel',
                            layout:'fit',
                            title:'兑换',
                            height:JE.getBodyHeight()-175,
                            width:810,
                            border:0,
                            default:{border:0},
                            items:[{xtype:'shop.shopcdkeyinfoview',vals:vals}]
                        }).show();
                    }else{
                        var form =me.down('panel[xtype=form]');
                        var passwordHint = form.getCmp('passwordHint');
                        var numberHint = form.getCmp('numberHint');//phoneNo
                        var phoneCodeHint=form.getCmp('phoneCodeHint');
                        if(res.code=='701'){
                            numberHint.update(res.message);
                        }else{
                            numberHint.update('');
                        }
                        if(res.code=='706'){
                            passwordHint.update(res.message);
                        }else{
                            passwordHint.update('');
                        }
                        if(res.code=='402'){
                            phoneCodeHint.update(res.message);
                        }else{
                            phoneCodeHint.update('');
                        }
                    }
                }
            })
        }
    }

});